package com.mallotec.android.timenote.ui.fragment.main;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.material.snackbar.Snackbar;
import com.leinardi.android.speeddial.SpeedDialView;
import com.mallotec.android.timenote.R;
import com.mallotec.android.timenote.data.bean.Timenote;
import com.mallotec.android.timenote.data.bean.comparator.TimenoteComparator;
import com.mallotec.android.timenote.databinding.FragmentHomeBinding;
import com.mallotec.android.timenote.ui.activity.TimenoteEdittingActivity;
import com.mallotec.android.timenote.ui.adapter.TimenoteAdapter;
import com.mallotec.android.timenote.ui.adapter.base.DiffTimenoteCallback;
import com.mallotec.android.timenote.ui.common.BaseFragment;
import com.mallotec.android.timenote.viewmodel.CustomViewModelFactoryProviders;
import com.mallotec.android.timenote.viewmodel.TimenoteListViewModel;
import com.mallotec.android.timenote.viewmodel.factory.TimenoteListModelFactory;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

public class HomeFragment extends BaseFragment <HomeFragment, FragmentHomeBinding> {

    private TimenoteListViewModel mViewModel;
    private TimenoteListModelFactory modelFactory;

    private static HomeFragment instance;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void onInit(Bundle savedInstanceState) {
    }

    @Override
    protected void onInitData(Bundle savedInstanceState) {
        // TODO: Use the ViewModel
        modelFactory = CustomViewModelFactoryProviders.getTimenoteListViewModelFactory(getContext());
        mViewModel = new ViewModelProvider(this, modelFactory).get(TimenoteListViewModel.class);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        binding.listTimenoteNotEnd.setLayoutManager(linearLayoutManager);
        TimenoteAdapter adapter = new TimenoteAdapter(R.layout.item_time_record, mViewModel);
        adapter.setDiffCallback(new DiffTimenoteCallback());
        binding.listTimenoteNotEnd.setAdapter(adapter);
        onSubscribeUi(adapter, binding);
    }

    @Override
    public HomeFragment getInstance() { // static? newInstance?
        if (instance == null) {
            instance = new HomeFragment();
        }
        return instance;
    }

    private void onSubscribeUi(TimenoteAdapter adapter, FragmentHomeBinding binding) {
        this.mViewModel.timenotesIsEnd.observe(this, timenotes -> {
            if (timenotes != null && adapter != null) {
                if (!mViewModel.dataInited) {
                    adapter.setNewInstance(timenotes);
                    mViewModel.dataInited = true;
                } else {
                    adapter.addData(timenotes);
                }
                Collections.sort(adapter.getData(), new TimenoteComparator());
                // 不 notify 可能会导致显示错误
                adapter.notifyDataSetChanged();
            }
        });
        binding.fabSpeedDial.setOnChangeListener(new SpeedDialView.OnChangeListener() {
            @Override
            public boolean onMainActionSelected() {
                mViewModel.dataInited = false;
                Intent intent = new Intent(getActivity(), TimenoteEdittingActivity.class);
                startActivity(intent);
                // Snackbar.make(binding.fragmentHome, "添加成功", Snackbar.LENGTH_SHORT).show();
                return false;
            }

            @Override
            public void onToggleChanged(boolean isOpen) {

            }
        });
        // 设置 item 点击事件
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            Timenote timenote = (Timenote) adapter1.getData().get(position);
            Intent intent = new Intent(getActivity(), TimenoteEdittingActivity.class);
            intent.putExtra("TimenoteId", timenote.getId());
            startActivity(intent);
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mViewModel.dataInited = false;
        TimenoteAdapter adapter = (TimenoteAdapter) binding.listTimenoteNotEnd.getAdapter();
        if (adapter != null) adapter.notifyDataSetChanged();
    }
}